<!DOCTYPE html>
<html lang="zh_CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <title>数据导入导出</title>
    <th:block th:include="~{common/common::commonHead}"/>
</head>
<body>
<div class="page-body">
    <div class="panel panel-primary shiny">
        <div class="panel-heading bg-blue">
            数据导入导出记录
        </div>
        <div class="widget-body grid-table">
            <div class="toolbar">
                <div class="fl-left">
                    <button data-menu-id="100401" data-click="exportData()" class="btn btn-blue shiny">导出数据</button>
                    <button data-menu-id="100402" data-click="importData()" class="btn btn-danger shiny">导入数据</button>
                </div>
                <div class="toolbar-right">
                    <div class="fl-right">
                        <div class="btn-group" role="group">
                            <button data-click="toggleFilter()" class="btn btn-default text-center">
                                <i class="fa fa-filter fa-fw"></i>
                            </button>
                            <button data-click="refresh('importExportList')" class="btn btn-default">
                                <i class="fa fa-refresh fa-fw"></i>
                            </button>
                            <button data-click="download('importExportList')" class="btn btn-default">
                                <i class="fa fa-download fa-fw"></i>
                            </button>
                        </div>
                    </div>
                </div>
            </div>
            <div id="importExportList"></div>
            <form class="filter search-content form-horizontal" role="form">
                <div class="col-xs-6">
                    <div class="form-group">
                        <label class="col-xs-4 control-label">类型：</label>
                        <div class="col-xs-8">
                            <div class="iconic-input right">
                                <select name="type" class="form-control">
                                    <option value="-1">全部</option>
                                    <option value="1">导出</option>
                                    <option value="2">导入</option>
                                </select>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="col-xs-6">
                    <div class="form-group">
                        <label class="col-xs-4 control-label">源：</label>
                        <div class="col-xs-8">
                            <div class="iconic-input right">
                                <select name="source" class="form-control">
                                    <option value="-1">全部</option>
                                    <option value="1">外网</option>
                                    <option value="2">内网</option>
                                </select>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="col-xs-6">
                    <div class="form-group">
                        <label class="col-xs-4 control-label">目标：</label>
                        <div class="col-xs-8">
                            <select name="target" class="form-control">
                                <option value="-1">全部</option>
                                <option value="1">外网</option>
                                <option value="2">内网</option>
                                <option value="3">银行</option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="col-xs-6">
                    <div class="form-group">
                        <label class="col-xs-4 control-label">监所：</label>
                        <div class="col-xs-8">
                            <select name="prisonId" class="form-control" data-url="org/listPrison" data-value-field="id" data-text-field="orgName">
                                <option value="-1">全部</option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="col-xs-12">
                    <button type="button" data-click="hideFilter()" class="btn btn-default pull-right">取消</button>
                    <button type="button" data-click="submitFilter('importExportList')" class="btn btn-blue pull-right">确定</button>
                </div>
            </form>
        </div>
    </div>

</div>
</body>
</html>
<script>
    $(function () {
        $("#importExportList").gridTable({
            url: "importExport/pageImportExport",
            columns: [
                {title: "编号", field: "id", show:false},
                {title: "类型", field: "type", format:typeFormatter},
                {title: "源", field: "source", format: environmentFormatter},
                {title: "目标", field: "target", format: environmentFormatter},
                {title: "监所", field: "prisonName"},
                {title: "序列号", field: "serialNumber"},
                {title: "操作人", field: "createUserName"},
                {title: "操作时间", field: "createTime"},
                {title: "重新下载", field: "downloadUrl", format: reDownloadFormatter}
            ],
            showControl: false,
            showCheckbox: false,
            multiSelect: false,
            onClickRow: null,
            onDoubleClickRow: null
        });
    });
    function typeFormatter(value) {
        switch (value) {
            case "1":
                return "<span class='label label-danger'>导入</span>";
            case "2":
                return "<span class='label label-success'>导出</span>";
            default:
                return value;
        }
    }
    function environmentFormatter(value) {
        switch (value) {
            case "1":
                return "<span class='label label-blue'>外网</span>";
            case "2":
                return "<span class='label label-sky'>内网</span>";
            case "3":
                return "<span class='label label-gold'>银行</span>";
            default:
                return value;
        }
    }
    function reDownloadFormatter(url, dx, row) {

        if (url === null) {
            return "";
        }
        var showName = row['sourceStr'] + "-" + row['targetStr'] + "-" + row['prisonName'] +"-"+row['serialNumber'];
        url += "?showName=";
        url += showName;
        url = encodeURI(url);
        return "<a class='text-primary' href='"+url+"' target='_blank' download='导出.zzg'>下载</a>"
    }

    function exportData() {
        var dialog = $.openIframeDialog({
            title: "导出数据",
            url: "system/exportData.html",
            buttons: {
                ok: {
                    text: "导出",
                    onclick: function () {
                        var doc = dialog.getSubPage();
                        var f = $("form", doc);
                        var validate = $(f).valid();
                        if (validate) {
                            var $target = $("#target", f);
                            var $prisonId = $("#prisonId", f);
                            var target = $target.val();
                            var prisonId = $prisonId.val();
                            var targetStr = $target.find("option[value='"+target+"']").text();
                            var prisonName = $prisonId.find("option[value='"+prisonId+"']").text();
                            var content = "";
                            if (targetStr === "内网") {
                                content = "<p>确认导出数据到[<span class='text-danger'>"+targetStr+"</span>]的[<span class='text-danger'>"+prisonName+"</span>]吗？</p>";
                            } else {
                                content = "<p>确认导出数据到[<span class='text-danger'>"+targetStr+"</span>吗？</p>";
                            }
                            content += "<p class='text-warning'>该操作将会弹出一个新窗口进行下载</p>";
                            dialog.close();
                            $.confirm({
                                title: "导出确认",
                                icon: "fa fa-question-circle-o",
                                content: content,
                                theme: "modern",
                                type: "orange",
                                buttons: {
                                    ok: {
                                        text: "确认导出",
                                        btnClass: "btn-success",
                                        action: function () {
                                            $.confirm({
                                                title: "导出成功",
                                                content: "如果文件下载出现问题可以在该页面重新下载",
                                                buttons: {
                                                    ok: {
                                                        text: "确认",
                                                        btnClass: "btn-success",
                                                        action: function () {
                                                            $$selection.get("gridTable", "importExportList").refresh();
                                                        }
                                                    }
                                                }
                                            });
                                            window.open("importExport/export?target="+target+"&prisonId="+prisonId);
                                        }
                                    },
                                    cancel: {
                                        text: "取消",
                                        btnClass: "btn-default"
                                    }
                                }
                            });
                        }
                    }
                }
            }
        });
    }
    function importData() {
        var dialog = $.openIframeDialog({
            title: "导入数据",
            url: "system/importData.html",
            buttons: {
                ok: {
                    text: "导入",
                    onclick: function () {
                        var doc = dialog.getSubPage();
                        var file = doc.getElementById("importFile").files[0];
                        var formData = new FormData();
                        formData.append("file", file);
                        $.ajax({
                            url: "importExport/import",
                            type: "POST",
                            data: formData,
                            processData : false,// 告诉jQuery不要去处理发送的数据
                            contentType : false,// 告诉jQuery不要去设置Content-Type请求头
                            beforeSend:function(){
                                console.log("正在进行，请稍候");
                            },
                            success : function(data) {
                                if (data.code !== '0') {
                                    $.error(data.msg);
                                } else {
                                    // 刷新列表
                                    $.success("导入成功");
                                    var gridTable = $$selection.get("gridTable", "importExportList");
                                    gridTable.refresh();
                                }
                            },
                            error : function() {
                                $.error("上传失败");
                            }
                        });
                        dialog.close();
                    }
                }
            }
        });
    }
</script>